Imports System.ComponentModel

<ToolboxBitmap(GetType(TextBox))> _
Public Class ACTextBoxAyuda
   Inherits TextBox

#Region " Variables "
   Public WithEvents ACAyuda As Button
   Public Event ACAyudaClick(ByVal sender As Object, ByVal e As EventArgs)

   Private m_rellenaCeros As Boolean
   Private m_tipo As TipoTextBoxAyuda
   Private m_longitudtext As Integer = 0
   Private m_activarControl As Boolean = False
   Private m_estadoControl As Boolean
   Private m_activarayuda As Boolean = True

   Public Enum TipoTextBoxAyuda
      Numerico
      AlfaNumerico
   End Enum
#End Region

#Region " Constructores "
   Public Sub New()
      MyBase.New()
      MyBase.Size = New System.Drawing.Size(100, 21)
      Try
         Me.m_rellenaCeros = False
         Me.m_tipo = TipoTextBoxAyuda.AlfaNumerico

         ACAyuda = New System.Windows.Forms.Button
         ACAyuda.Image = Global.ACControles.My.Resources.Resources.busqueda
         ACAyuda.UseVisualStyleBackColor = True
         ACAyuda.Size = New System.Drawing.Size(21, 21)
         ACAyuda.Dock = DockStyle.Right
         ACAyuda.Cursor = Cursors.Arrow
         ACAyuda.TabStop = False

         MyBase.Controls.Add(ACAyuda)
         ACAyuda.BringToFront()
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
#End Region

#Region " Propiedades Publicas "

   <Category("AcerosComerciales"), _
   DefaultValue(True), _
   Localizable(True), _
   Description("Tipo estado del contenido para ejecutar la busqueda.")> _
   Public ReadOnly Property ACEstadoAutoAyuda() As Boolean
      Get
         Return m_estadoControl
      End Get
   End Property

   <Category("AcerosComerciales"), _
   DefaultValue(TipoTextBoxAyuda.AlfaNumerico), _
   Localizable(True), _
   Description("Tipo de texto de ayuda.")> _
   Public Property ACTipo() As TipoTextBoxAyuda
      Get
         Return m_tipo
      End Get
      Set(ByVal value As TipoTextBoxAyuda)
         m_tipo = value
      End Set
   End Property

   <Category("AcerosComerciales"), _
   DefaultValue(False), _
   Localizable(True), _
   Description("Rellena de ceros el control.")> _
   Public Property ACRellenaCeros() As Boolean
      Get
         Return m_rellenaCeros
      End Get
      Set(ByVal value As Boolean)
         m_rellenaCeros = value
      End Set
   End Property

   <Category("AcerosComerciales"), _
   DefaultValue(True), _
   Localizable(True), _
   Description("Activar la auto ayuda segun el numero de caracteres aceptados.")> _
   Public Property ACActivarAyudaAuto() As Boolean
      Get
         Return m_activarControl
      End Get
      Set(ByVal value As Boolean)
         m_activarControl = value
         ACAyuda.Enabled = Not value
      End Set
   End Property

   <Category("AcerosComerciales"), _
   DefaultValue(3), _
   Localizable(True), _
   Description("Longitud aceptada para la autoaydua del control.")> _
   Public Property ACLongitudAceptada() As Integer
      Get
         Return m_longitudtext
      End Get
      Set(ByVal value As Integer)
         If Me.Text.Length >= value Then
            Me.Text = Me.Text.Substring(0, value)
            ACAyuda.Enabled = True
         Else
            ACAyuda.Enabled = False
         End If
         m_longitudtext = value
      End Set
   End Property

   <Category("Appearance"), _
   DefaultValue(""), _
   Localizable(True), _
   Description(".")> _
   Public Overrides Property Text() As String
      Get
         Return MyBase.Text
      End Get
      Set(ByVal value As String)
         MyBase.Text = value
      End Set
   End Property

   <Category("AcerosComerciales"), _
   DefaultValue(Integer.MaxValue), _
   Localizable(True), _
   Description("Maxima longitud aceptada.")> _
   Public Overrides Property MaxLength() As Integer
      Get
         Return MyBase.MaxLength
      End Get
      Set(ByVal value As Integer)
         MyBase.MaxLength = value
      End Set
   End Property

   Public Overrides Property Cursor() As System.Windows.Forms.Cursor
      Get
         Return MyBase.Cursor
      End Get
      Set(ByVal value As System.Windows.Forms.Cursor)
         MyBase.Cursor = value
      End Set
   End Property

   <Category("AcerosComerciales"), _
   DefaultValue(True), _
   Localizable(True), _
   Description("Activar la Ayuda, para el boton F1 y para cualquier operacion.")> _
   Public Property ACActivarAyuda() As Boolean
      Get
         Return m_activarayuda
      End Get
      Set(ByVal value As Boolean)
         m_activarayuda = value
      End Set
   End Property


   Public Shadows Property Tag() As Object
      Get
         Return MyBase.Tag
      End Get
      Set(ByVal value As Object)
         MyBase.Tag = value
      End Set
   End Property
#End Region

#Region " Propiedades Protegidas "
#Region " SobreEscritas "
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
      MyBase.Dispose(disposing)
   End Sub

   Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
      MyBase.OnGotFocus(e)
      If m_rellenaCeros Then
         MyBase.Text = Me.Text.PadLeft(Me.MaxLength, "0")
      End If
   End Sub

   Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
      MyBase.OnTextChanged(e)
   End Sub

   Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
      Const WM_KEYDOWN As Integer = 256
      Const WM_SYSKEYDOWN As Integer = 260
      If ((msg.Msg = WM_KEYDOWN) _
                  OrElse (msg.Msg = WM_SYSKEYDOWN)) Then
         Select Case (keyData)
            Case Keys.Tab
               Dim e As New KeyEventArgs(Keys.Tab)
               MyBase.OnKeyDown(e)
         End Select
      End If
      Return MyBase.ProcessCmdKey(msg, keyData)
   End Function
#End Region

   Protected Sub ACTextBoxAyuda_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
      Try
         If e.KeyCode = Keys.F1 And m_activarayuda Then
            If Me.Text.Trim().Length >= m_longitudtext And m_activarControl Then
               Me.ACAyuda_Click(sender, e)
            ElseIf Not m_activarControl Then
               Me.ACAyuda_Click(sender, e)
            End If
         End If
      Catch ex As Exception
         Throw ex
      End Try
   End Sub

   Protected Sub ACTextBoxAyuda_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
      Try
         If m_tipo = TipoTextBoxAyuda.Numerico Then
            Dim numberFormatInfo As Globalization.NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
            Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
            Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
            Dim negativeSign As String = numberFormatInfo.NegativeSign

            Dim keyInput As String = e.KeyChar.ToString()

            If [Char].IsDigit(e.KeyChar) Then
               ' Digits are OK
            ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
               ' Decimal separator is OK
            ElseIf e.KeyChar = vbBack Then
               ' Backspace key is OK
            Else
               ' Swallow this invalid key and beep
               e.Handled = True
            End If
         End If
      Catch ex As Exception
         Throw ex
      End Try
   End Sub

   Protected Sub ACAyuda_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ACAyuda.Click
      RaiseEvent ACAyudaClick(sender, e)
   End Sub

   Protected Sub ACActivarControlAutomatico(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
      Try
         If m_activarControl Then
            If Me.Text.Trim().Length >= m_longitudtext Then
               m_estadoControl = True
               ACAyuda.Enabled = True
            Else
               m_estadoControl = False
               ACAyuda.Enabled = False
            End If
         End If
      Catch ex As Exception
         Throw ex
      End Try
   End Sub
#End Region

End Class
